package com.alen.sun.service.Impl;

import com.alen.sun.bean.Admin;
import com.alen.sun.bean.Category;
import com.alen.sun.bean.PageBean;
import com.alen.sun.dao.CategoryMapper;
import com.alen.sun.service.CategoryService;
import com.alen.sun.util.PhoneCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 添加分类
     * @param category
     * @return
     */
    @Override
    public Boolean addCategory(Category category) {
        if(category.getCategoryName()!=null && !category.getCategoryName().isEmpty()){
            //参数不为空
            //先判断添加的分类是否已经存在
            Integer count = categoryMapper.selectByName(category);
            if(count == null || count ==0){
                //未查找到该分类,可以添加
                //生成categoryId
                while (true){
                    PhoneCode phoneCode = new PhoneCode();
                    String categoryId = phoneCode.generateCode();
                    //查询该id是否存在,直到循环到不存在
                    Integer count1 = categoryMapper.selectByCategoryId(categoryId);
                    if(count1 == null || count1 == 0){
                        //没有查询到这个id,可以使用
                        category.setCategoryId(categoryId);
                        //跳出循环
                        break;
                    }
                }
                //添加分类
                Boolean flag = categoryMapper.addCategory(category);
                return flag;
            }
        }
        return false;
    }

    /**
     * 修改分类信息
     * @param category
     * @return
     */
    @Override
    public Boolean updateCategory(Category category) {
        //判断接收的参数是否全不为空
        if(category.getCategoryId()!=null && !category.getCategoryId().isEmpty()
            && category.getCategoryName()!=null && !category.getCategoryName().isEmpty()){
            Boolean flag = categoryMapper.updateCategory(category);
            return flag;
        }
        return false;
    }

    /**
     * 删除分类
     * @param category
     * @return
     */
    @Override
    public Boolean deleteCategory(Category category) {
        Boolean flag = categoryMapper.deleteCategory(category);
        return flag;
    }

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageBean page(Integer page, Integer pageSize) {
        //1. 设置分页参数
        //计算参数start
        Integer start = (page-1)*pageSize;
        List<Category> categoryList = categoryMapper.page(start, pageSize);
        //2.查询总数
        Long count = categoryMapper.count();
        PageBean pageBean = new PageBean(count,categoryList);
        return pageBean;
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public Boolean deleteByIds(List<String> ids) {
        Boolean flag = categoryMapper.deleteByIds(ids);
        return flag;
    }

    @Override
    public List<Category> list() {
        List<Category> categories=categoryMapper.list();
        return categories;
    }

    @Override
    public List<Category> word(String searchName) {
        searchName="%"+searchName+"%";
        List<Category> categories=categoryMapper.word(searchName);
        return categories;
    }
}
